/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.board;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.TimeUtil;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.board.BoardRoom;
import com.unis.model.board.Conference;
import com.unis.service.board.BoardRoomService;
import com.unis.service.board.conferenceService;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-14
 */
@Controller
@RequestMapping("/boardroom")
public class BoardRoomController {

    private BoardRoomService boardroomService;
    private conferenceService conferenceService;
    /****************************************
     * Insert a instace of BoardRoom
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertBoardRoom(@RequestBody BoardRoom boardroom) throws Exception {
        boardroom.setStatmentId("BoardRoom.insert");
        String status = "failure";
        boardroomService.insert(boardroom);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of BoardRoom
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody BoardRoom boardroom) throws Exception {
        boardroom.setStatmentId("BoardRoom.update");
        String status = "failure";
        boardroomService.update(boardroom);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of BoardRoom
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody BoardRoom boardroom) throws Exception {
        boardroom.setStatmentId("BoardRoom.delete");
        String status = "failure";
        boardroomService.batchDelete(boardroom);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of BoardRoom
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        BoardRoom boardroom = new BoardRoom();
        boardroom.setStatmentId("BoardRoom.queryAsList");
        BeanUtils.populate(boardroom, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(boardroom, pageMap);
        List<BaseModel> boardroomList = boardroomService.queryAsList(boardroom);
        
        //查询并设置当前时间会议室状态
        Conference conf=new Conference();
        conf.setStatmentId("Conference.queryBoardRoomState");
        conf.setStarttime(TimeUtil.getNow());
        List<BaseModel> UsedBoardRooms=conferenceService.queryBoardRoomState(conf);
        for(BaseModel used : UsedBoardRooms){
            for(BaseModel each : boardroomList){
                BoardRoom UsedBoardRoom=(BoardRoom)used;
                BoardRoom br=(BoardRoom)each;
                if(UsedBoardRoom.getRoomid().equals(br.getRoomid())){
                    br.setNowstate("正被使用");
                    continue;
                }
            }
        }
        Map<String, Object> renderMap = GridUIMapper.render(boardroom, boardroomList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of BoardRoom
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody BoardRoom boardroom) throws Exception {
        boardroom.setStatmentId("BoardRoom.queryAsObject");
        return boardroomService.queryAsObject(boardroom);
    }

    @RequestMapping("/page")
    public String forword() {
        return "board/boardroomList";
    }

    @Resource
    public void setBoardRoomService(BoardRoomService boardroomService) {
        this.boardroomService = boardroomService;
    }
    
    @Resource
    public void setConferenceService(conferenceService conferenceService) {
        this.conferenceService = conferenceService;
    }
}
